
//////////////////////////////////////////////////////////////////////////////
/*
This file creates a measure of the break-even inflation rate for Argentina.
It combines 3 files: 01_Arg_Bonds_Data.dta, 01_Arg_ExchangeRate.dta and 02_Arg_merged.dta
	The 1st file has data on bond prices and yields for Argentina
	The 2nd file has data on NXR forwards
	The 3rd file has data on Argentina exchange rate, and dates in which government reported the inflation.
*/
//////////////////////////////////////////////////////////////////////////////

clear all
clear matrix
set more off
set maxvar 30000
set matsize 1000
set more off
local path_inputs 	= "00_data/01_datasets/00_inputs"


* Bonds that we use in the main analysis
*-----------------------------------------------------------------------------------------
local rate            = "Ask_Annual_Yield"
local IIB_bond_id     = 9                      // IIB; ISIN = ARARGE03E931 (only IIB for which we have good coverage)
local Dollars_bond_id = "84 36 28 82"          // Dollar bonds for which we have good coverage
local US_main_1       = 84                     // ISIN =  ARARGE03F342 
local US_main_2       = 36                     // ISIN =  ARARGE03F144 
*-----------------------------------------------------------------------------------------

	
*-----------------------------------------------------------------------------------------
// Load bond data and merge with NXR and inflation data for Argentina
use "`path_inputs'/01_Arg_Bonds_Data.dta", clear
qui merge m:1 Date using "`path_inputs'/02_Arg_merged.dta"
qui drop if Date<d(01jan2006)
qui drop if Date>=d(01jan2012)
qui drop _merge
qui sort Bond_ID Date
qui bysort Bond_ID : gen Date_ID = _n
qui sort Bond_ID Date
xtset Bond_ID Date_ID
qui  keep if Curr == "ARS" | Curr == "USD"


*-----------------------------------------------------------------------------------------
* Compute expected devaluation rate (12-month)
preserve
	use "`path_inputs'/01_Arg_ExchangeRate.dta" , clear
	
	sort Date
	gen Date_idd = _n
	tset Date_idd, daily
	
	* Compute annual expected devaluation based on forward NXR contracts
	qui gen devaluation_12 = 100*(Forward_12 /TC_Official-1)
	qui gen dev_change 	   = (devaluation_12-L.devaluation_12)

	* Compute moving average
	keep devaluation_12 Date_idd Date
	rename devaluation_12 devaluation
			qui gen xx = devaluation			
			local days_avg  = 5
			forval j=1/`days_avg' {
			qui replace xx = xx + L`j'.devaluation
			}
			qui gen devaluation_avg = xx/(`days_avg'+1)	
			qui  drop xx		
	save tmp_file.dta, replace
restore

merge m:1 Date using tmp_file.dta
drop _merge
qui sort Bond_ID Date_ID
erase tmp_file.dta
*-----------------------------------------------------------------------------------------



*-----------------------------------------------------------------------------------------
*-----------------------------------------------------------------------------------------
* COMPUTE THE BREAK EVEN INFLATION RATE
*-----------------------------------------------------------------------------------------
*-----------------------------------------------------------------------------------------

* Change yields to missing values after bond mature
qui replace Ask_Annual_Yield=. if Date[_n]>Maturity[_n]

* Keep only bonds that pay coupons semi-annually
keep if CpnFreqDes =="S/A"
				
	* Yield of Inflation Linked Bond
		preserve
		keep if Bond_ID==`IIB_bond_id'
		save file_tmp1.dta,replace
		restore

	* Yield of Bond in Dollars
		local i = 1
		foreach j in `Dollars_bond_id' {
			preserve
			tab Bond_ID
			keep if Bond_ID==`j'
			sum Date
			rename `rate'  `rate'_DP_`i'
			save file_tmp_US`i'.dta,replace
			restore
		local i = `i'+1
		}
		
	* Merge the datasets
		use file_tmp1.dta,clear
			local j = 1
			foreach i in `Dollars_bond_id' {
			merge 1:1 Date using file_tmp_US`j'
			drop _merge
			erase file_tmp_US`j'.dta
			local j = `j'+1
			}
		erase file_tmp1.dta
		
	*Compute the yields differential across bonds
		local j = 1
		foreach i in `Dollars_bond_id' {
		gen Yield_diff_`j' = `rate'_DP_`j'  -`rate'
		local j = `j'+1
		}
		
	*Compute the break-even inflation rate
		local j = 1
		foreach i in `Dollars_bond_id' {
		gen BE_`j' =  Yield_diff_`j'  + devaluation_avg
		local j = `j'+1
		}
		
	*Compute the average break-even inflation rate using the baseline US bonds
	gen Yield_diff = 0.5*Yield_diff_1 + 0.5*Yield_diff_2
	gen BE         = 0.5*BE_1 + 0.5*BE_2
		
	*Save		 
	keep Date devaluation_avg devaluation BE Yield_diff BE_* Yield_diff_* EMBI_* Inf_Misreport Inf_Misreport_Change   Misreport_Annualized  INDEC_Annualized IPC_INDEC Merval_px
	save "`path_inputs'/03_BreakEvenInflation.dta", replace
*-----------------------------------------------------------------------------------------
*-----------------------------------------------------------------------------------------
